package cn.yr.blog.controller.web;

import cn.hutool.core.util.ObjectUtil;
import cn.yr.blog.service.ArticleService;
import cn.yr.common.core.domain.dto.YrArticleTagDto;
import cn.yr.common.core.domain.entity.YrArticle;
import cn.yr.common.core.domain.vo.PageVo;
import cn.yr.common.core.utils.IpUtil;
import cn.yr.common.exception.ret.Ret;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 文章(YrArticle)表控制层
 *
 * @author lang
 * @since 2020-03-05 20:47:46
 */

@Api(tags = "web文章")
@RestController
@RequestMapping("/web/article")
public class WebArticleController {

    @Resource
    private ArticleService articleService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * @author     ：lang
     * @description ： 文章物理分页
     */
    @RequestMapping("/pageArticle")
    public Ret<PageVo<YrArticleTagDto>> pageArticle(@RequestBody PageVo<YrArticleTagDto> pageVo){
        Map<String, Object> params = pageVo.getParams();
        QueryWrapper<YrArticleTagDto> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("art.create_time");
        //按标签查
        wrapper.eq(ObjectUtil.isNotEmpty(params.get("tagId")),"tag.id",params.get("tagId"));
        wrapper.eq(ObjectUtil.isNotEmpty(params.get("type")),"art.type",params.get("type"));
        wrapper.eq(ObjectUtil.isNotEmpty(params.get("recommend")),"art.recommend",params.get("recommend"));
        PageVo<YrArticleTagDto> articleIPage = articleService.pageArticle(pageVo,wrapper);
        return  Ret.success(articleIPage);
    }

    /**
     * @author     ：lang
     * @description： 根据ID查询文章
     */
    @RequestMapping("/getArticleById/{id}")
    public Ret getArticleById(@PathVariable("id") @NotNull(message = "文章id不能为空") Integer id){
        YrArticleTagDto yrArticleTagDto = articleService.getArticleById(id);
        return Ret.success(yrArticleTagDto);
    }

    /**
     * @description 增加阅读数
     * @author Lang
     * @date 2020/8/2 17:50
     */
    @RequestMapping("/addArticleReadNum/{id}")
     public Ret addArticleReadNum(@PathVariable("id") @NotNull(message = "文章id不能为空") Integer id, HttpServletRequest request){

        String ip = IpUtil.getRealIP(request);
        String redisKey = "articleRead:"+ip+":"+id;
        Object o = redisTemplate.opsForValue().get(redisKey);
        if(ObjectUtil.isNotEmpty(o)){
            return Ret.success();
        }
        redisTemplate.opsForValue().set(redisKey, "ljy:"+ip,1,TimeUnit.DAYS);
        YrArticle yrArticle = articleService.getById(id);
        yrArticle.setReadNum(yrArticle.getReadNum()+1);
        articleService.updateById(yrArticle);
        return Ret.success();
    }
}